﻿add2LOV = function(id,value)
{
    var textbox = $id(id);    var panel = $id("lovadd_" + id);    var lovname = $id("lovn_" + id).value;    var custom = $id("lovp_" + id);    var customparam = "";        if( custom != null )    {        customparam = custom.value;    }        var url = "ASHX/LOV.ashx?lovname=" + escape(lovname) +         "&action=addnew" +         "&cid=" + escape(id) +         "&customparam=" + escape(customparam) +         "&value=" + escape(value);            var div = $id("popuplayer");    div.style.display = "block";    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    renderFirst();        var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {                    div.innerHTML = ajx.responseText;        }    }}

insert2LOV = function(id)
{
    var textbox = $id(id);    var lovname = $id("lovn_" + id).value;    var select = $id("lovslc_" + id);    var selected = "";    var input = $id("lovtxt_" + id).value;        var custom = $id("lovp_" + id);    var customparam = "";        if( custom != null )    {        customparam = custom.value;    }        if( select != null )    {        selected = select.options[select.selectedIndex].value;    }    var url = "ASHX/LOV.ashx?lovname=" + escape(lovname) +         "&action=insert" +         "&cid=" + escape(id) +         "&datatype=" + escape(selected) +         "&customparam=" + escape(customparam) +         "&value=" + escape(input);            var div = $id("popuplayer");    div.style.display = "block";    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    renderFirst();        var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {            chooseLOV(id, ajx.responseText, input);            closePopupLayer();        }    };}

delayLOV = function(id,index,size)
{
    setTimeout("drawLOV('" + id + "','" + index + "','" + size + "')",110);
}

drawLOV = function(id,index,size)
{
    index = index == undefined ? "" : index;
    size = size == undefined ? "" : size;

    var filter = $id("lov_" + id).value;
    var lovname = $id("lovn_" + id).value;
    var hf = $id("lovh_" + id);
    
    var custom = $id("lovp_" + id);    var customparam = "";        if( custom != null )    {        customparam = custom.value;    }
        
    var url = "ASHX/LOV.ashx?lovname=" + escape(lovname) + 
        "&action=draw" +         "&cid=" + escape(id) + 
        "&filter=" + escape(filter) + 
        "&customparam=" + escape(customparam) +         "&index=" + index + 
        "&size=" + size;
    
    hf.value = "";
    
    var content = $id("lovc_" + id);
    content.style.display = "block"; 
    content.innerHTML = "<img src='" + LOADING.src + "' alt='' />"; 
    renderFirst(); // do this to render the loading image first
    
    var ajx = sendAJAX(url,true);
    
    ajx.onreadystatechange = function() {
        if (ajx.readyState == 4) {
            content.innerHTML = ajx.responseText;
        }
    }
}

focusLOV = function(id)
{
    drawLOV(id);
    
    var elmt = $id("lov_" + id);
    elmt.select();    
}

withdrawLOV = function(id, customaction)
{
    var match = $id("lovmatch_" + id);
    var input = $id("lov_" + id);
    var hf = $id("lovh_" + id);
    var textbox = $id(id);
    
    if( match != null )
    {        
        var arrmatch = match.value.split(DELIMITER);
        setTimeout("chooseLOV('" + id + "','" + arrmatch[0] + "','" + arrmatch[1] + "','" + customaction + "')",200);
    }
    else
    {
        setTimeout("hideLOV('" + id + "')",200);
        input.style.backgroundColor = "#FFAAAA";
        try
        {        
            textbox.disabled = true;
        }
        catch(err)
        {}
    }
}

enterLOV = function(id, customaction)
{
    var evt = (evt) ? evt : ((event) ? event : null);
    
    if( evt.keyCode == 13 ) 
    {
        var input = $id("lov_" + id);
        if(input.blur())
        {}
        else{input.onblur();}
    }
}
hideLOV = function(id)
{
    var content = $id("lovc_" + id);
    content.style.display = "none"; 
    content.innerHTML = "";    
}

chooseLOV = function(cid, opid, opvalue, customaction)
{
    var textbox = $id(cid);
    var input = $id("lov_" + cid);
    var hf = $id("lovh_" + cid);
    var datatype = $id("lovdt_" + cid);

    input.value = opvalue;
    hf.value = opid;
    
    if(hf.value == "")
    {
        input.style.backgroundColor = "#FFAAAA";
        textbox.disabled = true;
        input.focus();
    }
    else
    {
        input.style.backgroundColor = "#FFFFFF";
        try
        {
            textbox.disabled = false;
            textbox.focus();
        }
        catch(err)
        {}
    }
    
    if( datatype != null )
    {
        changeTextboxType(cid);
    }
    
    if( customaction != undefined && customaction != "" )
    {
        try
        {
            setTimeout(customaction,100);        
        }
        catch(err)
        {}
    }
    
    hideLOV(cid);
}

changeTextboxType = function(id)
{
    var textbox = $id(id);    var lovname = $id("lovn_" + id).value;    var hf = $id("lovh_" + id);
    var datatype = $id("lovdt_" + id);
    var url = "ASHX/LOV.ashx?lovname=" + escape(lovname) +         "&action=geteditor" +         "&cid=" + escape(id) +        "&olddt=" + escape(datatype.value) +        "&value=" + escape(hf.value);        
    
    var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {                        if( ajx.responseText == "" )            {                return;            }                        var splitted = ajx.responseText.split(DELIMITER);            var dt = textbox.parentNode;            datatype.value = splitted[0];                        if( dt != null )            {                dt.innerHTML = splitted[1];            }        }    }
}